﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PcapDotNet.Core;
using System.Collections;
using System.Text.RegularExpressions;

namespace WNistnet
{
    public static class Init
    {
        private static IList<LivePacketDevice> allDevices = null;
        private static IList<RouteItem> routeTable = null;
        //取网卡
        public static IList<LivePacketDevice> ListAllDevice()
        {
            allDevices = LivePacketDevice.AllLocalMachine;
            if (allDevices.Count == 0)
            {
                return null;
            }
            return allDevices;
        }

        public static IList<ArpItem> InitArpTable()
        {
            IList<ArpItem> arpTable = new List<ArpItem>();
            //通过命令行获得arp -a的返回结果
            string result = Util.GetCmdResult("arp -a");
            //用正则取出mac与ip的列表
            MatchCollection mc = Regex.Matches(result, @"\d+.\d+.\d+.\d+\s+\w\w-\w\w-\w\w-\w\w-\w\w-\w\w");
            foreach (Match item in mc)
            {
                //去掉空格
                string temp = item.Value.Replace(" ", "");
                //前半断是IP，后半断是MAC
                string macAddress = temp.Substring(temp.Length - 17).Replace("-", ":");
                string ipAddress = temp.Substring(0, temp.Length - 17);
                ArpItem arpItem = new ArpItem();
                arpItem.MacAddress = macAddress;
                arpItem.IpAddress = ipAddress;
                arpTable.Add(arpItem);
            }
            return arpTable;
        }

        public static IList<RouteItem> InitRouteTable()
        {
            return routeTable;
        }
    }
}
